package com.shunda.ysdgj.manager.service.impl.goods;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.shunda.ysdgj.common.exception.CustomException;
import com.shunda.ysdgj.common.util.ValidateUtil;
import com.shunda.ysdgj.manager.mapper.goods.GoodsBrandCategoryMapper;
import com.shunda.ysdgj.manager.service.goods.GoodsBrandCategoryService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.dto.goods.GoodsBrandCategoryFindDto;
import com.shunda.ysdgj.model.entity.goods.GoodsBrand;
import com.shunda.ysdgj.model.entity.goods.GoodsBrandCategory;
import com.shunda.ysdgj.model.entity.goods.GoodsCategory;
import com.shunda.ysdgj.model.vo.ResultCodeEnum;
import com.shunda.ysdgj.model.vo.goods.GoodsBrandCategoryVo;
import com.shunda.ysdgj.model.vo.goods.GoodsBrandVo;
import com.shunda.ysdgj.model.vo.goods.GoodsCategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsBrandCategoryImpl
        extends MPJBaseServiceImpl<GoodsBrandCategoryMapper, GoodsBrandCategory>
        implements GoodsBrandCategoryService, MPJBaseService<GoodsBrandCategory> {

    public List<GoodsBrandVo> findBrandByCategoryId(Long id) {
        if (ValidateUtil.isNotLong(id)) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        MPJLambdaWrapper<GoodsBrandCategory> baseWrapper = JoinWrappers
                .lambda(GoodsBrandCategory.class)
                .select(GoodsBrand::getId, GoodsBrand::getBrandName)
                .leftJoin(GoodsBrand.class, GoodsBrand::getId, GoodsBrandCategory::getBrandId)
                .eq(GoodsBrandCategory::getCategoryId, id);
        return getBaseMapper().selectJoinList(GoodsBrandVo.class, baseWrapper);
    }

    public List<GoodsCategoryVo> findByBrandId(Long id) {
        if (ValidateUtil.isNotLong(id)) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        MPJLambdaWrapper<GoodsBrandCategory> baseWrapper = JoinWrappers
                .lambda(GoodsBrandCategory.class)
                .select(GoodsCategory::getId, GoodsCategory::getCategoryName)
                .leftJoin(GoodsCategory.class, GoodsCategory::getId, GoodsBrandCategory::getCategoryId)
                .eq(GoodsBrandCategory::getBrandId, id);
        return getBaseMapper().selectJoinList(GoodsCategoryVo.class, baseWrapper);
    }

    public IPage<GoodsBrandCategoryVo> findByPage(GoodsBrandCategoryFindDto goodsBrandCategoryFindDto) {
        if (ValidateUtil.isNotLong(goodsBrandCategoryFindDto.getCurrent())) {
            goodsBrandCategoryFindDto.setCurrent(1L);
        }
        if (ValidateUtil.isNotLong(goodsBrandCategoryFindDto.getSize())) {
            goodsBrandCategoryFindDto.setSize(10L);
        }
        MPJLambdaWrapper<GoodsBrandCategory> baseWrapper = JoinWrappers
                .lambda(GoodsBrandCategory.class)
                .select(
                        GoodsBrandCategory::getId, GoodsBrandCategory::getBrandId,
                        GoodsBrandCategory::getCategoryId, GoodsBrandCategory::getCreated
                ).select(GoodsBrand::getBrandName, GoodsBrand::getBrandLogo)
                .select(GoodsCategory::getCategoryName)
                .innerJoin(GoodsBrand.class, GoodsBrand::getId, GoodsBrandCategory::getBrandId)
                .innerJoin(GoodsCategory.class, GoodsCategory::getId, GoodsBrandCategory::getCategoryId);
        if (ValidateUtil.isLong(goodsBrandCategoryFindDto.getBrandId())) {
            baseWrapper.eq(GoodsBrandCategory::getBrandId, goodsBrandCategoryFindDto.getBrandId());
        }
        if (ValidateUtil.isLong(goodsBrandCategoryFindDto.getCategoryId())) {
            baseWrapper.eq(GoodsBrandCategory::getCategoryId, goodsBrandCategoryFindDto.getCategoryId());
        }
        IPage<GoodsBrandCategoryVo> page = new Page<>(goodsBrandCategoryFindDto.getCurrent(), goodsBrandCategoryFindDto.getSize());
        return getBaseMapper().selectJoinPage(page, GoodsBrandCategoryVo.class, baseWrapper);
    }

    public boolean add(GoodsBrandCategory goodsBrandCategory) {
        if (ValidateUtil.isNotLong(goodsBrandCategory.getBrandId())
                || ValidateUtil.isNotLong(goodsBrandCategory.getCategoryId())) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        goodsBrandCategory.setId(null);
        return save(goodsBrandCategory);
    }

    public boolean modify(GoodsBrandCategory goodsBrandCategory) {
        if (ValidateUtil.isNotLong(goodsBrandCategory.getId())
                || ValidateUtil.isNotLong(goodsBrandCategory.getBrandId())
                || ValidateUtil.isNotLong(goodsBrandCategory.getCategoryId())) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        return updateById(goodsBrandCategory);
    }

    @Transactional
    public boolean deleteByIds(IdsDto idsDto) {
        List<Long> ids = idsDto.getIds();
        if (ids.isEmpty()) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        return removeByIds(ids);
    }

}
